package com.gps.track;



import java.util.List;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.CornerPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.Point;

import com.datatypes.GeoPtr;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

public class DrawPathOverlay extends Overlay {

	Paint paint;
	final List<GeoPtr> pointList;
	String speedStr = "";
	boolean ghost = false;
	boolean Ighost;
	boolean draw;
	MapController contorller;

	boolean stop = false;

	public DrawPathOverlay(List<GeoPtr> points, boolean ghost, boolean draw,
			boolean Ighost, MapController contorller, int color) {
		pointList = points;
		this.ghost = ghost;
		this.draw = draw;
		this.contorller = contorller;
		this.Ighost = Ighost;

		paint = new Paint();
		paint.setAntiAlias(true);
		paint.setStyle(Paint.Style.STROKE);
		paint.setPathEffect(new CornerPathEffect(10));

		if (ghost) {
			paint.setColor(color);
			paint.setStrokeWidth(8);
			paint.setAlpha(200);
		} else {
			paint.setColor(color);
			paint.setStrokeWidth(5);
			paint.setAlpha(90);
		}

	}

	private void drawCircle(Canvas canvas, Paint paint, Point point,
			boolean isStart) {
		Paint circlePaint = new Paint(paint);
		if (isStart)
			circlePaint.setStyle(Paint.Style.STROKE);
		else
			circlePaint.setStyle(Paint.Style.FILL_AND_STROKE);
		circlePaint.setStrokeWidth(isStart ? 4 : 2);
		canvas.drawCircle(point.x, point.y, 6, circlePaint);
	}

	public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
			long when) {

		if (!draw)
			return false;
		else {

			/** update the speed and the map location */
			if (!pointList.isEmpty()) {
				GeoPtr last = pointList.get(pointList.size() - 1);
				if (!Ighost) {
					if (!stop)
						contorller.setCenter(toGeoPoint(last));
				}
			}
			Projection projection = mapView.getProjection();
			if (shadow == false && pointList != null) {
				Point startPoint = null, endPoint = null;
				Path path = new Path();

				/**
				 * create the path
				 */
				for (int i = 0; i < pointList.size(); i++) {
					GeoPoint gPointA = toGeoPoint(pointList.get(i));
					Point pointA = new Point();
					projection.toPixels(gPointA, pointA);
					if (i == 0) {
						startPoint = pointA;
						path.moveTo(pointA.x, pointA.y);
					} else if (i == pointList.size() - 1) {
						endPoint = pointA;
					}
					path.lineTo(pointA.x, pointA.y);

				}
				if (startPoint != null) {
					drawCircle(canvas, paint, startPoint, true);
				}
				if (endPoint != null) {
					drawCircle(canvas, paint, endPoint, false);
				}
				if (!path.isEmpty())
					canvas.drawPath(path, paint);

			}
			return super.draw(canvas, mapView, shadow, when);
		}
	}

	public static GeoPoint toGeoPoint(GeoPtr gpt) {
		return new GeoPoint((int) (gpt.getLatitude() * 1E6),
				(int) (gpt.getLongitude() * 1E6));
	}

}
